﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;


public partial class _1003_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request["mes_no"] != null && Request["mes_no"] != "")
            {
                this.hidd_no.Value = Request["mes_no"];
                this.LoadData();
            }

            if (Request["sfu_no"] != null && Request["sfu_no"] != "")
            {
                this.hidd_sfuNo.Value = Request["sfu_no"];
            }
            else
            {
                this.hidd_sfuNo.Value = "2001";
            }

            //標題名稱 <a href="">首頁</a><img src="css/tem/img/img-icon-4.jpg" width="5" height="7" />最新消息
            this.nav.InnerHtml = "";
            string imgStr = "<img src=\"css/tem/img/img-icon-4.jpg\" width=\"5\" height=\"7\" />";

            string[] sfu_name =  new sysfunctionDAO().Get_titleName(int.Parse(this.hidd_sfuNo.Value)).Split('/');

            if (sfu_name[0].Trim() == sfu_name[1].Trim())
            {
                this.nav.InnerHtml = string.Format("<a href=\"index.aspx\">首頁</a>{0}<a href=\"1003.aspx?sfu_type=" + Request["sfu_no"] + "\">{1}</a>{2}<a href=\"#\"><b>訊息內容</b></a>", imgStr, sfu_name[1].Trim(), imgStr);
            }
            else
            {
                this.nav.InnerHtml = string.Format("<a href=\"index.aspx\">首頁</a>{0}{1}{2}<a href=\"1003.aspx?sfu_type=" + Request["sfu_no"] + "\">{3}</a>{4}<b>訊息內容</b>", imgStr, sfu_name[0].Trim(), imgStr, sfu_name[1].Trim(), imgStr);
            }

            //this.lab_title2.Text = sfu_name[1].Trim();
        }
    }

    private void LoadData()
    {
        int mes_no = 0;
        int.TryParse(this.hidd_no.Value, out mes_no);

        if (mes_no > 0)
        {
            message data = new messageDAO().Get_message(mes_no);
            this.lab_claname.Text = data.cla_no.Value.GetClaType().ConverToCN();
            this.lab_subject.Text = data.mes_title.ConverToCN();
            this.lab_date.Text = data.mes_datetime.Value.ConvertToShowDT();
            this.div_content.InnerHtml = data.mes_content;
        }
        else
        {
            this.lab_claname.Text = "";
            this.lab_date.Text = "";
            this.lab_subject.Text = "";
            this.div_content.InnerHtml = "";
        }

        this.ObjectDataSource1.SelectParameters[0].DefaultValue = mes_no.ToString();
        this.GridView1.DataBind();
    }

    protected string getUrl(object val)
    {
        return "~/messageFile.ashx?file_no=" + (int)val;
    }

    private void NextData(int type)
    {
        if (this.hidd_no.Value != "")
        {
            messageDAO dao = new messageDAO();

            var data = dao.Get_message_AllData(int.Parse(this.hidd_sfuNo.Value), 0).ToArray();

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].mes_no == int.Parse(this.hidd_no.Value))
                {
                    if (type == 1 && i == data.Length-1)
                    {
                        JsUtil.AlertJs(this, "此為最後一則訊息!");
                        break;
                    }
                    if (type == 1 && ((i + 1) < data.Length))
                    {
                        this.hidd_no.Value = data[i + 1].mes_no.ToString();
                        this.LoadData();
                        break;
                    }

                    if (type == 2 && i == 0)
                    {
                        JsUtil.AlertJs(this, "此為第一則訊息!");
                        break;
                    }
                    if (type == 2 && ((i - 1) < data.Length))
                    {
                        this.hidd_no.Value = data[i - 1].mes_no.ToString();
                        this.LoadData();
                        break;
                    }

                }
            }

        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.NextData(2);
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        this.NextData(1);
    }
}